package UIFree.controls
{
	import UIFree.uif_app;
	import UIFree.uif_component;
	import UIFree.uif_ecomponent;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	
	public class uif_hint2 extends uif_label {
		private var _curFocus	: uif_component;
		private var _eventID	: int;
		private var _target		: Point;
		
		public function uif_hint2(parent: uif_component, styleClass: Object) {
			var rect: Rectangle = parent.getInner();
			super(parent, rect.x, rect.y, rect.width, rect.height, styleClass);
			setVisibility(false);
		}
		
		override protected function listenEvents():void {
			super.listenEvents();
			app.addEventListener(uif_app.HINT_OVER, function(e: MouseEvent): void {
				if (This.curFocus != e.target) This.curFocus = e.target as uif_component;
			});			
			
			app.addEventListener(uif_app.HINT_OUT, function(e: MouseEvent): void {
				if (This.curFocus) This.curFocus = null;
			});
		}
		
		public function get curFocus() : uif_component {
			return _curFocus;
		}
		
		public function set curFocus(a_curFocus : uif_component): void {
			if (_curFocus) removeEvent(_eventID);
			_curFocus = a_curFocus;
			if (_curFocus && _curFocus.hint && (_curFocus.hint is String)) {
				setText(_curFocus.hint as String);
				_target		= globalToLocal(_curFocus.localToGlobal(_curFocus.center));
				slideShow();	
				_eventID = addEvent(uif_component.UPDATEHINT, doUpdateHint, _curFocus);
			} else {
				slideHide();
			}
		}
		
		protected function setText(a_text: String): void {
			htmlText 	= _curFocus.hint as String;
			height 		= textField.textHeight + getSpace(true) * 2;
		}
		
		protected function doUpdateHint(e: Event): void {
			htmlText = (e.target as uif_component).hint as String;
		}
	}
}